function slideshow() {
  $('.slides .slide-content img').reflect({ 'height': 0.25, 'opacity': 0.25 });
  
  //CSS3 Support for older browsers
  $('#featured-headlines li:nth-child(2)').css('display', 'block');

  function showTitle() {
  //makes corresponding headline match slideshow image
    var currentAlt = $('#featured-stories .content-slider .slides .big .reflected').attr('alt');
    var titles = $('ul#featured-headlines li');
    titles.each(function() {
      if (jQuery.trim($(this).text()) === currentAlt) {
        $(this).show();
        } else {
        $(this).hide();
      }
    });
  }

  //Controls Featured Stories homepage slideshow
  $('#featured-stories .slideshow-buttons a').live('click', function() {
    var container = $('#featured-stories .slides .slide-content').not(':animated');
    if (!container.length) {
      return false;
    }
    var slides = '#featured-stories .content-slider .slide-image .reflected';
    var possibleMoves = $(slides).length;
    var firstElement = $(slides+':first');
    var lastElement = $(slides+':last');
    var currentElement = $('#featured-stories .content-slider .slides .big .reflected');
    var nextElement = "";
    
    if ($(this).hasClass('next')) {
      nextElement = currentElement.parent().next().find("img");
      if (nextElement.length == 0) {
        nextElement = firstElement;
        delta = 151;
        } else {
        delta = 207;
        }
        } else if ($(this).hasClass('prev')) {
        nextElement = currentElement.parent().prev().find("img");
        if (nextElement.length == 0) {
          nextElement = lastElement;
        }
        delta = 151;
      }

      container.animate({
        'left': (-1 * $(nextElement).position().left + delta) + 'px'
      });

      currentElement.parent().animate({
        'width': '134px',
        'height': '262px',
        'marginTop': '30px'
      });
      currentElement.animate({
        'width': '134px',
        'height': '134px'
      });
      currentElement.next().animate({
          'width': '134px'
      });
      nextElement.parent().animate({
        'width': '190px',
        'height': '341px',
        'marginTop': '0'
      });
      nextElement.animate({
        'width': '190px',
        'height': '190px',
        'marginTop': '0'
      });
      nextElement.next().animate({
          'width': '190px'
      });

      currentElement.parent().removeClass('big').addClass('thumb');
      nextElement.parent().removeClass('thumb').addClass('big');
      
      showTitle();

      return false;
      
  });
  
}
  

function repsTrLink() {
  //Makes entire .reps tr link to rep profile
  $("table.reps tr:not(tr.head)").click(function(){
    window.location=$(this).find("h4 a").attr("href");
    return false;
  });
}


function addressSearch() {
  //Hides form#address-search label onclick
  var $search = $('#address-search').addClass('overlabel');
  var $searchInput = $search.find('input.search-input');
  var $searchLabel = $search.find('label');
  
  if ($searchInput.val()) {
    $searchLabel.hide();
  }
  
  $searchInput.focus(function() {
    $searchLabel.hide();
  })
  
  .blur (function() { 
    if (this.value == '') {
      $searchLabel.show();
    }
  });
  
  $searchLabel.click(function() {
    $searchInput.trigger('focus');
  });
}
  

function fileType() {
  //add classes to document download links based on filetype
  $('a[href$=.pdf]').parent().addClass('pdf-download');
  $('a[href$=.doc]').parent().addClass('word-download');
  $('a[href$=.xls]').parent().addClass('excel-download');
}


function contentTabs() {
  //New tabs attempt
  $("#tab-nav li a").click(function() {
    // Figure out current list via CSS class
    var curList = $("#tab-nav li a.current").attr("rel");

    // List moving to
    var $newList = $(this);

    // Set outer wrapper height to height of current inner list
    var curListHeight = $("#tabbed-panels").height();
    $("#tabbed-panels").height(curListHeight);

    // Remove highlighting - Add to just-clicked tab
    $("#tab-nav li a").removeClass("current");
    $newList.addClass("current");

    // Figure out ID of new list
    var listID = $newList.attr("rel");

    if (listID != curList) {
       // Fade out current list
       $("#"+curList).fadeOut(50, function() {
           // Fade in new list on callback
           $("#"+listID).fadeIn(50);
           // Adjust outer wrapper to fit new list snuggly
           var newHeight = $("#"+listID).height();
           $("#tabbed-panels").animate({
               height: newHeight
           });
       });
    }        

    // Don't behave like a regular link
    return false;
  });
}

function collapseContent() {
  // Toggles collapsable content  
  // hide all of the elements with hidden-content
  $("#primary-content .hidden-content").hide();

  // toggle the components with class hidden-content and adds content-displayer-open class to corresponding head
  $("#primary-content .content-displayer").click(function() {
    $(this).toggleClass("content-displayer-open")
    $(this).next("#primary-content .hidden-content").slideToggle(400);  
  });
}

function modalOverlay() {
  //Thanks to Kevin Liew. http://www.queness.com
  //select all the a tag with name equal to modal
  $('a[name=modal]').click(function(e) {
  	//Cancel the link behavior
  	e.preventDefault();
	
  	//Get the A tag
  	var id = $(this).attr('href');

  	//Get the screen height and width
  	var maskHeight = $(document).height();
  	var maskWidth = $(window).width();

  	//Set heigth and width to mask to fill up the whole screen
  	$('#mask').css({'width':maskWidth,'height':maskHeight});
	
  	//transition effect		    
  	$('#mask').fadeIn("1000");	
  	//$('#mask').fadeTo("1000",0.5);	

  	//Get the window height and width
  	var winH = $(window).height();
  	var winW = $(window).width();
            
  	//Set the popup window to center
  	$(id).css('top',  winH/2-$(id).height()/2);
  	$(id).css('left', winW/2-$(id).width()/2);

  	//transition effect
  	$(id).fadeIn("fast"); 

  });

	//if close button is clicked
	$('.modal a.modal-close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.modal').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.modal').hide();
	});
}

function readMore() {
  //Hides long paragraphs until read-more link is clicked
  var slicePoint = 130;
  var widow = 4;
  $('#primary-content div.abbreviated p').each(function() {
    var allText = $(this).html();
    var startText = allText.slice(0,slicePoint).replace(/\w+$/,'');
    var endText = allText.slice(startText.length);
    if ( endText.replace(/\s+$/,'').split(' ').length> widow ) {
      $(this).html([
        startText,
        '<a href="#" class="read-more">read more...</a>',
        '<span class="details">',
          endText,
        '</span>'
        ].join('')
      );
    }
  });

	// *** hide details until read-more link is clicked;
  // then hide link and show details.
  $('span.details').hide();
  $('a.read-more').click(function() {
    $(this).hide()
    .next('span.details').fadeIn();
    return false;
  });
}

function viewAllTags() {
  //Hides long lists of tags until view-all link is clicked
  var slicePoint = 130;
  var widow = 4;
  $('#primary-content dl.postmetadata dd').each(function() {
    var allText = $(this).html();
    var startText = allText.slice(0,slicePoint).replace(/\w+$/,'');
    var endText = allText.slice(startText.length);
    if ( endText.replace(/\s+$/,'').split(' ').length> widow ) {
      $(this).html([
        startText,
        '<a href="#" class="view-all">view all tags...</a>',
        '<span class="details">',
          endText,
        '</span>'
        ].join('')
      );
    }
  });

	// *** hides tags until view-all link is clicked;
  // then hide link and show details.
  $('span.details').hide();
  $('a.view-all').click(function() {
    $(this).hide()
    .next('span.details').fadeIn();
    return false;
  });
}

// to be called on every page load
$(function() {
  addressSearch();
  fileType();
  collapseContent();
  
  // Removes border from .important-report's preceding sibling. When CSS3 rules the school, we won't need to do this with jQuery.
  $("div.important-report").prev("div").css("border","none");
  //$("#primary-content h4").prev("h3").css("margin-bottom", "10px"); 

  //calls inFieldLabels for forms that use it
  $("label").inFieldLabels();
});
